module.exports = { // 字节转字符串
  bytes2Str(arrBytes) {
    var str = "";
    for (var i = 0; i < arrBytes.length; i++) {
      var tmp;
      var num = arrBytes[i];
      if (num < 0) { // 此处填坑，当byte因为符合位导致数值为负时候，需要对数据进行处理
        tmp = (255 + num + 1).toString(16);
      } else {
        tmp = num.toString(16);
      }
      if (tmp.length == 1) {
        tmp = "0" + tmp;
      }
      if (i > 0) {
        str += ":" + tmp;
      } else {
        str += tmp;
      }
    }
    return str;
  },
  // 字符串转16进制
  str2HexCharCode(str) {
    if (str === "") 
      return "";
    
    var hexCharCode = [];
    hexCharCode.push("0x");
    for (var i = 0; i < str.length; i++) {
      hexCharCode.push((str.charCodeAt(i)).toString(16));
    }
    return hexCharCode.join("");
  },
  str2ArrayBuffer(value) {
    const buffer = new ArrayBuffer(value.length / 2)
    const dataView = new DataView(buffer);
    const arr = [];
    for (let i = 0; i < value.length; i += 2) {
      arr.push(`0x${value.slice(i, i + 2)}`);
    }
    arr.forEach((a, idx) => {
      dataView.setUint8(idx, a);
    })
    return Buffer.from(buffer);
  },
  parseLatLng(str) {
    // 7b:00:16:a2:01:31:32:31:2e:35:38:38:32:33:36:2c:33:38:2e:39:38:38:30:32:32:bd:7d:1a
    const arr = str.split(':').slice(5);
    let lat = '', lng = '', isLat = false;
    for (let i = 0; i < arr.length; i++) {
      const item = arr[i];
      if (item[0] === '3') {
        if (isLat) {
          lat += arr[i][1];
        } else  {
          lng += arr[i][1];
        }
      } else if (item === '2e') {
        isLat ? (lat += '.') : (lng += '.');
      } else if (item === '2c') {
        isLat = true;
      }
    }
    return (lat && lng) ? [lat, lng] : null;
  }
}
